import java.util.HashMap;
import java.util.Optional;

/**
 * 多数元素
 */
public class MajorityElement {
    public static int solution(int[] nums) {
        HashMap<Integer,Integer> map = new HashMap<>(nums.length);
        int k = nums.length / 2;
        for (int num : nums) {
            int times = Optional.ofNullable(map.get(num)).orElse(0);
            if (times + 1 > k){
                return num;
            }
            map.put(num,times+1);
        }
        return 0;
    }

    public static void main(String[] args) {
       int[] nums = {2,2,1,1,1,2,2};
        System.out.println(solution(nums));
    }
}
